package com.fireline.beauty.cashier;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.widgets.TextPopup;

/**
 * 产品信息查找帽泡对话框
 * */
public class CpxxTextPopup extends TextPopup {

	private Table table;
	String filter; // 过滤条件
	/**
	 * 产品信息查找
	 * 
	 * @wbp.nonvisual location=27,482
	 */
	private final Dataset dataset = new Dataset(true, "cpxxCz",
			SqliteJdbcImpl.INSTANCE);

	public CpxxTextPopup(Text control, Listener selection) {
		super(control, selection);
		dataset.update();
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	protected Control createContent(Composite parent) {
		// dataset.setKeyColumns(new String[] {});
		Composite content = new Composite(parent, SWT.BORDER);
		GridLayout gl_content = new GridLayout();
		gl_content.verticalSpacing = 0;
		gl_content.marginWidth = 0;
		gl_content.marginHeight = 0;
		gl_content.horizontalSpacing = 0;
		content.setLayout(gl_content);
		table = new Table(content, SWT.FULL_SELECTION);

		table.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				if (table.getSelectionCount() > 0) {
					okPressed();
				}
			}
		});
		GridData gd_table = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_table.heightHint = 0;
		gd_table.verticalIndent = -1;
		table.setLayoutData(gd_table);
		table.setHeaderVisible(true);

		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setResizable(false);
		tblclmnNewColumn.setWidth(70);
		tblclmnNewColumn.setText("产品编号");

		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(100);
		tblclmnNewColumn_1.setText("产品名称");

		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setWidth(60);
		tblclmnNewColumn_2.setText("单价");

		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(100);
		tableColumn.setText("库存数量");

		Label label = new Label(content, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		Composite composite = new Composite(content, SWT.NONE);
		RowLayout rl_composite = new RowLayout(SWT.HORIZONTAL);
		rl_composite.marginTop = 0;
		rl_composite.spacing = 10;
		composite.setLayout(rl_composite);
		GridData gd_composite = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		gd_composite.verticalIndent = 5;
		gd_composite.horizontalIndent = -1;
		composite.setLayoutData(gd_composite);

		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.setText("选择");

		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.setText("关闭");
		setParams();
		return content;
	}

	public void doFilter(String f) {
		table.removeAll();
		for (int i = 0; i < dataset.getCount(); i++) {
			dataset.setSelected(i);
			String cpbh = dataset.getString("cpbh");
			String cpmc = dataset.getString("cpmc");
			String jm = dataset.getString("jm");
			String dj = dataset.getString("sj");
			String kc = dataset.getString("kcsl");
			if (cpbh != null && cpbh.contains(f)
					|| (cpmc != null && cpmc.contains(f))
					|| (jm != null && jm.contains(f))) {
				TableItem item = new TableItem(table, SWT.NONE);
				item.setText(new String[] { cpbh, cpmc, dj, kc });
			}
		}
		if (table.getItemCount() > 0)
			table.setSelection(0);
	}

	public void doSelectUp() {
		int index = table.getSelectionIndex() - 1;
		if (index >= 0 && index < table.getItemCount())
			table.setSelection(index);
	}

	public void doSelectDown() {
		int index = table.getSelectionIndex() + 1;
		if (index >= 0 && index < table.getItemCount())
			table.setSelection(index);
	}

	@Override
	protected void okPressed() {
		if (table.getSelectionIndex() != -1) {
			dataset.setSelected(table.getSelectionIndex());
			Event event = new Event();
			event.data = dataset;
			hide();
			okListener.handleEvent(event);
		}
	}

	@Override
	protected Point getSize() {
		return new Point(364, 278);
	}

	protected void setParams() {
	}
}
